/**
 * author: zhangwei
 * 用户管理新增、编辑
 */
var pageType = "add";
var main = {

    init: function () {
        layui.use('element', function () {
        });
        function submitCode() {
            var code = $("textarea").val();
            $("#result").html(code);

        }

        pageType = localStorage.userType;
        if (pageType == 'edit') {
            $('#submit').text('保存');
            // this.getDetailData();
            this.id = localStorage.userId;
            loadData(this.id);
        }
        this.initTable();
    },
    initTable: function () {
        var _this = this;
        layui.use('form', function () {
            var form = layui.form;


            //监听提交
            form.on('submit(formSubmit)', function (data) {
                var data = $("#userForm").serializeArray();
                var jsonObj={};
                $(data).each(function(){
                    jsonObj[this.name]=this.value;
                });
                $.ajax({
                    url: '/user/saveUser',
                    async: false,
                    data: JSON.stringify(jsonObj),
                    type: 'post',
                    dataType:'json',
                    contentType:'application/json',
                    success: function (result) {
                        if (result.code == property.successCode) {
                            if (pageType == "edit") {
                                successMsg("修改用户成功");
                            } else {
                                successMsg("添加用户成功");
                            }
                            parent.$t.goback("/user/manage");
                        } else{
                            errorMsg(result.msg);
                        }
                    },
                    error: function (result) {
                        errorMsg("系统异常");
                    }
                });
                return false;
            })
        });
        //监听重置
        $("#cancel").click(function () {
            layer.confirm('确认取消吗?', function (index) {
                parent.$(".myRefresh").click();
                layer.close(index);
            });
            return false;
        });

    },
}
main.init();


/**
 * 加载表单数据
 * @param id 角色id
 */
function loadData(id) {
    this.type = "edit";
    layui.use('form', function () {
        var form = layui.form;
        var index = parent.layer.getFrameIndex(window.name);
        var json = {"userId":id};
        //加载数据
        $.ajax({
            type:"get",
            data:json,
            async:false,
            url:"/user/getSysUserById",
            success:function(result) {
                if (result.code == property.successCode) {
                    setFormData(result.data);
                    // form.render('select');
                } else {
                    errorMsg(result.msg);
                }
            },
            error:function(result) {
                errorMsg("系统异常");
            }
        });
    });
}

/**
 * 设置表单数据
 * @param data
 */
function setFormData(data) {
    property.setForm($("#userForm"), data);

}




